<?php
$q=$_GET["q"];
$params=explode(",",$q);
$year=$params[0];
$Exam_Name=$params[1];
$Subject=$params[2];
require_once ("../Login/config.php");
include_once ("../Login/databaseconnection.php");
$imageWidth =950;
$imageHeight =300;
$diameter = 250;
$centerX =450;
$centerY = 150;
$labelWidth =25; 
$chartTitle =$Exam_Name." ".$Subject." ".$year; 
$schools=mysql_db_query($db,"SELECT DISTINCT(School_Id) FROM exam WHERE Year='".$year."' AND Subject='".$Subject."' AND Exam_Name='".$Exam_Name."'");
$query2 = "select sum(No_Of_Passed_Students) as countammount from exam WHERE Year='".$year."' AND Subject='".$Subject."' AND Exam_Name='".$Exam_Name."'";
$result2 = mysql_db_query($db,$query2);


while ($row = mysql_fetch_assoc ($result2)) 
{
$dataTotal = $row[countammount];
}

function circlePoint( $deg, $dia )
{
$x = cos( deg2rad( $deg ) ) * ( $dia / 2 );
$y = sin( deg2rad( $deg ) ) * ( $dia / 2 );
return array( $x, $y );
}
$num_schools = mysql_num_rows($schools); 
$im = ImageCreate( $imageWidth, $imageHeight ); 
for($c=1;$c<=$num_schools;$c++)
{
$color[] = ImageColorAllocate( $im,50*$c,60*$c,20*$c);
}
$white = ImageColorAllocate( $im, 255, 255, 255 );
$black = ImageColorAllocate( $im, 0, 0, 0 );
$grey = ImageColorAllocate( $im, 215, 215, 215 );

ImageFill( $im, 0, 0,$white ); 

$degree = 0;

$i=-1;
$s_no=0;
while ($rowsc = mysql_fetch_assoc ($schools))
{
$s_no++;
$sch=$rowsc['School_Id'];
$query1 = "select sum(No_Of_Passed_Students) as groupcount from exam WHERE Year='".$year."' AND School_Id='".$sch."' AND Subject='".$Subject."' AND Exam_Name='".$Exam_Name."'";
$result1 = mysql_db_query($db,$query1);
$query_sat_students = "select sum(No_Of_Sat_Students) as satcount from exam WHERE Year='".$year."' AND School_Id='".$sch."' AND Subject='".$Subject."' AND Exam_Name='".$Exam_Name."'";
$result_sat_students = mysql_db_query($db,$query_sat_students);
if($rows_s=mysql_fetch_array($result_sat_students))
{
$satcount=$rows_s['satcount'];
}
$result3 = mysql_db_query($db,$query1);
$num_products = mysql_num_rows($result1); 

$currentColor = $color[ $i % ( count( $color ) )];
while ($row = mysql_fetch_assoc ($result1))
{
$i < sizeof( $row ); $i++;
$startDegree = round( $degree );
$degree += ( $row['groupcount'] / $dataTotal ) * 360;
$endDegree = round( $degree );


ImageArc( $im, $centerX, $centerY, $diameter, $diameter, $startDegree, $endDegree, $currentColor );

list( $arcX, $arcY ) = circlePoint( $startDegree, $diameter );
ImageLine( $im, $centerX, $centerY, floor( $centerX + $arcX ), floor( $centerY + $arcY ), $currentColor );

list( $arcX, $arcY ) = circlePoint( $endDegree, $diameter );
ImageLine( $im, $centerX, $centerY, ceil( $centerX + $arcX ), ceil( $centerY + $arcY ), $currentColor );

$midPoint = round( ( ( $endDegree - $startDegree ) / 2 ) + $startDegree );
list( $arcX, $arcY ) = circlePoint( $midPoint, $diameter / 1.5 );
ImageFillToBorder( $im, floor( $centerX + $arcX ), floor( $centerY + $arcY ), $currentColor, $currentColor );
ImageString( $im,3, floor( $centerX + $arcX - 26 ), floor( $centerY + $arcY - 6 ), intval( round( $row[groupcount] / $dataTotal * 100 ) ) . "% (".$s_no.")", $white );
}

$i < sizeof( $rowsc ); $i++; 
ImageRectangle( $im, $labelX, $labelY, $labelX + $labelWidth, $labelY + $labelWidth, $black );
ImageFilledRectangle( $im, $labelX + 1, $labelY + 1, $labelX + $labelWidth, $labelY + $labelWidth, $currentColor );
$scid=$rowsc['School_Id'];
$getSch=mysql_query("SELECT * FROM school WHERE School_Id='".$scid."'");
if($rs=mysql_fetch_array($getSch))
{
$sname=$rs['Name'];
}
ImageString( $im, 3, $labelX + $labelWidth + 15, $labelY,$sname."(".$s_no.")", $black );
$labelY += $labelWidth + 5;

}
$labelX = $centerX + $diameter / 2 + 10;
$labelY = $centerY - $diameter / 4;
$titleX = $labelX - $diameter / 4;
$titleY = $centerY - $diameter / 2;
ImageString( $im, 3, $titleX, $titleY, $chartTitle, $black );

ImageString( $im, 4, $labelX, $labelY, "Total number Of sat students:", $black );
ImageString( $im, 4, $labelX + $labelWidth + 220, $labelY, $satcount, $black );
ImageString( $im, 4, $labelX, $labelY+20, "Total number Of passed students:", $black );
ImageString( $im, 4, $labelX + $labelWidth + 240, $labelY+20, $dataTotal, $black );
ImageString( $im, 2, $labelX, $labelY + 15, $logo, $black );
Header( "Content-type: image/jpg" ); 
Imagejpeg( $im );
ImageDestroy( $im );
?>
